//
//  CFBTHomePageVC.m
//  半糖框架
//
//  Created by afro on 15/12/11.
//  Copyright © 2015年 afro. All rights reserved.
//

#import "CFBTHomePageVC.h"
#import "CFBTBannerView.h"
#import "CFBTBannerModel.h"
#import "CFBTTopicModel.h"
#import "CFBTAllModel.h"
#import "CFBTTopicTableViewCell.h"
#import "CFBTGetDataTool.h"
#import "CFBTLandscapeTabBarView.h"
#import "CFBTTopicTableVC.h"
#import "CFBTCategoryElementModel.h"
#import "NSDictionary+propertyList.h"


@interface CFBTHomePageVC ()<UIScrollViewDelegate,CFBTLandscapeTabBarViewDataSource>

/*-------------------------------bannerView-------------------------------------------*/

// 最底部ScrollView
@property (weak, nonatomic) IBOutlet UIScrollView *hoemPageMainScrollView;

// 图片轮播器bannerView
@property (strong, nonatomic) IBOutlet CFBTBannerView *bannerView;

// bannerView高度和顶部约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bannerHeightConst;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bannerTopConst;

// 最开始y轴偏移量
@property (nonatomic ,assign) CGFloat oriOffsetY;

/*-------------------------------bannerData-------------------------------------------*/

// 图片轮播器(banner)模型数组
@property (strong, nonatomic) NSMutableArray *bannersArray;

// 图片轮播器图片URL数组
@property (strong, nonatomic) NSMutableArray *bannerImageURL;

/*-------------------------------category_element-------------------------------------*/

// banner下方View
@property (weak, nonatomic) IBOutlet UIView *categoryView;

/**菜单栏*/
@property (strong, nonatomic) CFBTLandscapeTabBarView *categoryTabBarView;

// 分类菜单项显示的标题
@property (strong, nonatomic) NSMutableArray *categoryArr;

/**分类标题*/
@property (strong, nonatomic) NSMutableArray *categoryTitleArr;

/*-------------------------------topic-------------------------------------*/

@property (strong, nonatomic) NSMutableArray *topicArr;

/**显示topic内容的控制器*/
@property (strong, nonatomic) CFBTTopicTableVC *topicVC;

/**所有的模型*/
@property (strong, nonatomic) CFBTAllModel *allMode;

@end

@implementation CFBTHomePageVC

#pragma mark - 资源懒加载
- (NSMutableArray *)bannersArray {
    if (_bannersArray == nil) {
        _bannersArray = [NSMutableArray array];
    }
    
    return _bannersArray;
}

- (NSMutableArray *)bannerImageURL {
    if (_bannerImageURL == nil) {
        _bannerImageURL = [NSMutableArray array];
    }
    
    return _bannerImageURL;
}

// 设置菜单项显示标题
- (NSMutableArray *)categoryArr {
    
    if (_categoryArr == nil) {
        
        _categoryArr = [NSMutableArray array];
    }
    
    return _categoryArr;
}

// 设置菜单项显示标题
- (NSMutableArray *)categoryTitleArr {
    
    if (_categoryTitleArr == nil) {
        
        _categoryTitleArr = [NSMutableArray array];
    }
    
    return _categoryTitleArr;
}

- (NSMutableArray *)topicArr {
    if (_topicArr == nil) {
        
        _topicArr = [NSMutableArray array];
    }
    
    return _topicArr;
}

- (CFBTAllModel *)allMode {
    if (_allMode == nil) {
        _allMode = [[CFBTAllModel alloc] init];
    }
    return _allMode;
}
#pragma mark - View加载完毕
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    // 1.获取网络数据
    [self getHomePageResource];
    
    // 2.创建首页最底层的ScrollView
    [self setupHoemPageScrollView];
    
    // 3.创建横向滚动的分类菜单栏 + 内容TableView
    //[self setupCategoryBar];
    
    // 不需要给TableView添加额外的滚动区域
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // 进入主页首先禁用横向滚动的TableView
    //self.categoryView.userInteractionEnabled = NO;
    
    
}

#pragma mark - 获取首页网络数据
// 1.获取首页网络数据
- (void)getHomePageResource {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [[CFBTGetDataTool shareCFBTGetDataTool] dataRequestWithURL:banTangRequestURL requestType:GET success:^(CFBTAllModel *data) {
        
        self.allMode = data;
        // 拿到模型
        self.bannersArray = [data valueForKey:@"banner"];
        self.topicArr = [data valueForKey:@"topic"];
        self.categoryArr = [data valueForKey:@"category_element"];
        // 第一页为最新
        [self.categoryTitleArr addObject:@"最新"];
        for (CFBTCategoryElementModel *category in self.categoryArr) {
            NSString *title = category.title;
            [self.categoryTitleArr addObject:title];
        }
        // 2.2 从banner数据中筛选出图片URL
        for (CFBTBannerModel *banner in self.bannersArray) {
            
            NSURL *url = [NSURL URLWithString:banner.photo];
            
            [self.bannerImageURL addObject:url];
            
        }
        
        // 2.3 将banner图片的URL传入CFBTInfiniteScrollView创建图片轮播器
        self.bannerView.images = self.bannerImageURL;
        [MBProgressHUD hideHUDForView:self.view animated:NO];
        // !!!请求完网络数据,再创建横向滚动的tableView
        [self setupCategoryBar];
        
    } fail:^(NSError *error) {
        //NSLog(@"获取网络数据失败!");
        [MBProgressHUD showError:@"获取网络数据失败" toView:self.view];
    }];
}
// 2.创建首页最底层的ScrollView
- (void)setupHoemPageScrollView {

    self.hoemPageMainScrollView.delegate = self;
    
    // 初始偏移量
     _oriOffsetY = -(HeadY + CategoryBarH);
    
    self.hoemPageMainScrollView.contentInset = UIEdgeInsetsMake(HeadY + CategoryBarH, 0, 0, 0);
    
    self.hoemPageMainScrollView.contentSize = CGSizeMake(0, 2000);
    
    // 设置导航条文字
    UILabel *label = [[UILabel alloc] init];
    label.text = @"半糖";
    
    // 自适应大小
    [label sizeToFit];
    
    // 文字初始颜色为透明
    label.textColor = [UIColor colorWithWhite:0 alpha:0];
    
    // 设置导航条文字
    self.navigationItem.titleView = label;
    

    
}
// 3.创建横向滚动的分类菜单栏 + 内容TableView
- (void)setupCategoryBar {
    
    // 1.创建横向滚动菜单栏 + 内容tableView
    CFBTLandscapeTabBarView *landscapeView = [[CFBTLandscapeTabBarView alloc] init];
    
    // 2.设置frame
    landscapeView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    landscapeView.datasource = self;
    
    // 3.设置每个菜单项要显示的标题
    landscapeView.titleArr = self.categoryTitleArr;
    self.categoryTabBarView = landscapeView;
    [self.categoryView addSubview:landscapeView];
    
}
// 4.自定义数据源方法,设置菜单栏下边的tableView每一个cell内要显示的内容
- (UITableView *)selectTabBar:(CFBTLandscapeTabBarView *)landscapeTabBarView
                      AtIndex:(NSUInteger)index{
    // 4.1  内容tableView的第一个Cell
    if (index == 0) {
        
        if (self.topicVC == nil) {
        
            CFBTTopicTableVC *pageVC = [[CFBTTopicTableVC alloc] init];
            pageVC.tableView.frame = CGRectMake(0,0,kScreenWidth,kScreenHeight);
            pageVC.topicArr = self.topicArr;
            
            self.topicVC = pageVC;
        }
        
        return self.topicVC.tableView;

    } else {
        // 4.2 这里添加其他页内容
        return nil;
    }
    
}
#pragma mark - 处理拖动事件
// 分类菜单栏悬停
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    // 拖动时首先停止banner计时器
    [self.bannerView stopTheTimer];
    
    // 获取当前偏移量 ,当前滚动到的位置
    CGFloat offsetY = scrollView.contentOffset.y;
    
    // 计算tableView滚动距离
    CGFloat delta = offsetY - _oriOffsetY;
    
    // banner图像的高度
    CGFloat height = (HeadY - delta);
    
    // 大于200说明是向下滚动,否则向上滚动
    if (height > 200) {
        
        // banner不改变高度
        height = 200;
        
        // banner随父控件向下拖动而移动
        self.bannerTopConst.constant = - delta;
    }
    
    // 悬停,减1是为了配合上边使用,有1个像素的线
    if (height < CategoryBarH + StatusBarH - 1) {
        height = CategoryBarH + StatusBarH - 1;
        
    }
    // 视觉差效果
    self.bannerHeightConst.constant = height;
    
    // 处理导航栏透明度
    CGFloat alpha = delta  / (HeadY - 64);
    
    if (alpha >= 1) {
        alpha = 0.99;
    }
    
    // 确定导航栏颜色
    UIColor *redColor = [UIColor colorWithRed:236/255.0 green:82/255.0 blue:82/255.0 alpha:alpha];
    
    // 生成对应的图片
    UIImage *image = [UIImage imageWithColor:redColor];
    
    // 设置导航栏透明度
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    
    // 设置导航栏标题文字颜色
    UILabel *label = (UILabel *)self.navigationItem.titleView;
    label.textColor = [UIColor colorWithWhite:0 alpha:alpha];

}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    // 停止拖动时,开始计时器
    [self.bannerView startTheTimer];

}
@end


